package com.bilibili.juc.cas;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.concurrent.atomic.AtomicStampedReference;


@ToString
@NoArgsConstructor
@AllArgsConstructor
@Data
class MyBook{
    private int id;
    private String bookname;
}


public class MyAtomicStampledDemo {

    public static void main(String[] args) {
        MyBook javabook = new MyBook(1,"javaBook");

        AtomicStampedReference<MyBook> asr = new AtomicStampedReference<MyBook>(javabook,1);

        System.out.println(asr.getReference()+"\t" + asr.getStamp());


        MyBook mysqlbook = new MyBook(2,"mysqlBook");


        boolean b = asr.compareAndSet(javabook,mysqlbook,asr.getStamp(),asr.getStamp()+1);

        System.out.println(b+"\t"+asr.getReference()+"\t" + asr.getStamp());

        boolean b2 = asr.compareAndSet(mysqlbook,javabook,asr.getStamp(),asr.getStamp()+1);

        System.out.println(b+"\t"+asr.getReference()+"\t" + asr.getStamp());





    }
}
